package com.lanou.dllo.shise.com.lanou.dllo.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.Food_magazineListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.entity.FoodBean;
import com.lanou.dllo.shise.com.lanou.dllo.entity.FoodZineBean;
import com.lanou.dllo.shise.com.lanou.dllo.entity.SearchBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/31.
 */
public class MyFragment extends Fragment {
    private RequestQueue queue;
    private RecyclerView fragmentfoodzineView;
    private Food_magazineListAdapter adapter;
    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_foodzine_view,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }



    private void initView() {
        queue = Volley.newRequestQueue(getActivity());
        fragmentfoodzineView = (RecyclerView) getActivity().findViewById(R.id.fragmentfoodzineView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1);
        fragmentfoodzineView.setLayoutManager(gridLayoutManager);
        adapter = new Food_magazineListAdapter(getActivity());
        fragmentfoodzineView.setAdapter(adapter);

    }
    private void initData() {
        String url = "http://api.meishixing.com/foodzine/weekly/list/city_id=7&state=2&page=1";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FoodZineBean foodBean = gson.fromJson(response, FoodZineBean.class);
                if (foodBean != null) {
                    List<FoodZineBean.ResultEntity> fr = foodBean.getResult();
                    if (fr != null && fr.size() > 0 ){
                       adapter.addData(fr);

                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(request);

    }
}
